const Koa = require('koa')
const app = new Koa()
const router = require('./routes')
const views = require('koa-views')
const serve = require('koa-static')
const u = require('./api')

// 获取师资团队类型
let teacherType = []
u.getTeacherType({
  code: 'SZFL'
}).then(({ errno, errmsg, result }) => {
  if (errno == 0) {
    teacherType = result.dictDatas
  }
})

// 挂载静态页面并载入ejs
app.use(views(__dirname + '/views', {
  map: {
    html: 'ejs'
  }
}))

// 访问静态资源
app.use(serve(__dirname + '/static'))

// 中间件
app.use(async (ctx, next) => {
  const userAgent = ctx.request.header['user-agent'] || '';
  const source = ctx.query.source || '';
  // const host = 'http://192.168.0.224:3001';
  // const host = 'http://192.168.31.18:3001';
  const host = `https://${ctx.request.header.host}`;
  teacherType.forEach(item => {
    item.title = item.name
    item.checked = false
    item.href = `${host}/${item.note}/`
  })
  ctx.config = {
    host,
    version: '1.0.3',
    title: '元翔职校',
    description: '元翔职校',
    keyword: '元翔职校',
    source,
    nav: [{
      id: 1,
      title: '网站首页',
      href: `${host}/`,
      checked: false
    }, {
      id: 2,
      title: '走进元翔',
      href: '',
      checked: false,
      children: [{
        id: 1,
        title: '公司概况',
        href: `${host}/gsgk/`
      }, {
        id: 2,
        title: '荣誉资质',
        href: `${host}/ryzz/`
      }]
    }, {
      id: 3,
      title: '新闻中心',
      href: '',
      checked: false,
      children: [{
        id: 1,
        title: '元翔动态',
        href: `${host}/yxdt/`
      }, {
        id: 2,
        title: '行业动态',
        href: `${host}/hydt/`
      }]
    }, {
      id: 4,
      title: '培训中心',
      href: '',
      checked: false,
      children: [{
        id: 1,
        title: '机动车驾驶培训',
        href: `${host}/jdcjspx/`
      }, {
        id: 2,
        title: '特种作业培训',
        href: `${host}/tzzypx/`
      }, {
        id: 3,
        title: '道路运输资格培训',
        href: `${host}/dlyszgpx/`
      }]
    }, {
      id: 6,
      title: '师资团队',
      href: '',
      checked: false,
      children: teacherType
      // children: [{
      //   id: 1,
      //   title: '优秀教师',
      //   href: `${host}/yxjs/`
      // }]
    }, {
      id: 7,
      title: '联系我们',
      href: '',
      checked: false,
      children: [{
        id: 1,
        title: '报名地址',
        href: `${host}/bmdz/`
      }, {
        id: 2,
        title: '校车路线',
        href: `${host}/xclx/`
      }]
    }],
    navMobile: [{
      title: '网站首页',
      href: `${host}/`,
      src: `${host}/imgs/nav1.png`
    }, {
      title: '走进元翔',
      href: `${host}/gsgk/`,
      src: `${host}/imgs/nav2.png`
    }, {
      title: '新闻中心',
      href: `${host}/yxdt/`,
      src: `${host}/imgs/nav3.png`
    }, {
      title: '培训中心',
      href: `${host}/jdcjspx/`,
      src: `${host}/imgs/nav4.png`
    }, {
      title: '师资团队',
      href: `${host}/gltd/`,
      src: `${host}/imgs/nav5.png`
    }, {
      title: '联系我们',
      href: `${host}/bmdz/`,
      src: `${host}/imgs/nav6.png`
    }],
    banner: 'https://static.dzyxzx.cn/client/img/banner1.jpg',
    url: ctx.url,
    teacherType
  }

  await next()
  // 404
  if (ctx.response.status == 404) {
    ctx.redirect('/404.html')
  }
})

// 挂载路由
app.use(router.routes()).use(router.allowedMethods())

// 配置服务
app.listen(3001, () => {
  // console.log('server is running http://localhost:3001')
})